package net.dopan.pigframe.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.dopan.pigframe.core.base.controller.response.ResponseBean;
import net.dopan.pigframe.demo.mapper.HomePageMapper;
import net.dopan.pigframe.demo.service.HomepageService;
import net.dopan.pigframe.demo.service.impl.ribbon.ActivityServiceRibbon;
import net.dopan.pigframe.demo.service.impl.ribbon.PersonalServiceRibbon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController

@RequestMapping("/homepage")
@Api(tags = "homepageController", description = "主页")
public class HomepageController {

    @Autowired
    private HomepageService homepageService;
    @Autowired
    private PersonalServiceRibbon personalServiceRibbon;
    @Autowired
    private ActivityServiceRibbon activityServiceRibbon;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private HomePageMapper homePageMapper;

    @ApiOperation("添加好友(包括同意添加好友)")
    @GetMapping("/addFriend")
    @Transactional
    public ResponseBean addFriends(@RequestParam("userId") String userId, @RequestParam("friendId") String friendsId) {
        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        if (userId.equals(friendsId)) {
            return new ResponseBean(ResponseBean.FAIL, "不可以添加自己为好友!", "");
        }
        if (homepageService.checkRelationship(userId, friendsId)) {
            return new ResponseBean(ResponseBean.SUCCESS, "已经是您好友!", "");
        }
        if (!homepageService.checkConcern(userId, friendsId)) {
            /** 添加关注 */
            homepageService.concernUser(userId, friendsId);
            //  String str = personalServiceRibbon.concernUser(userId, friendsId);

        }
        return new ResponseBean(ResponseBean.SUCCESS, "发送添加好友请求!", "");
    }

    @ApiOperation("获取好友列表")
    @GetMapping("/getFriends")
    @Transactional
    public ResponseBean getFriends(@RequestParam("userId") String userId) {
        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        return new ResponseBean(ResponseBean.SUCCESS, "", homepageService.getFriends(userId));
    }

    @GetMapping("/getUserEvent")
    @ApiOperation("获取好友动态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String")})
    public ResponseBean getUserEvent(@RequestParam("userId") String userId, @RequestParam("pageSize") String pageSize, @RequestParam("pageNum") String pageNum) {
        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        int offset, limit;
        try {
            offset = (Integer.parseInt(pageNum) - 1) * Integer.parseInt(pageSize);
            limit = Integer.parseInt(pageSize);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new ResponseBean(ResponseBean.FAIL, "pageNum和pageSize为数字", "");
        }

        List<Map<String, String>> list = homepageService.getUserEvents(userId, offset, limit);
        boolean flag = true;
        if (list.size() < limit) {
            flag = false;
        }
        int i = 0;
        for (Map<String, String> list1 : list
        ) {
            Map map = new HashMap();
            map.put("open1", "false");
            map.put("open2", "false");
            map.put("comment_h", "22");
            map.put("comment_id", "c" + (++i));
            map.put("user_view_id", "u" + i);
            map.put("my_user_id", userId);
            list1.putAll(map);
        }

        return new ResponseBean(ResponseBean.SUCCESS, flag + "", list);
    }

    @GetMapping("getActivityDetail")
    @ApiOperation("获取活动详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "activityId", value = "活动Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String")
    })
    public ResponseBean getActivityDetail(@RequestParam("activityId") String activityId, @RequestParam("userId") String userId) {
        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        return new ResponseBean(ResponseBean.SUCCESS, "", activityServiceRibbon.getActivityDetail(activityId, userId));
    }

    @PostMapping("getUser")
    public ResponseBean getUser(@RequestParam("username")String username,@RequestParam("password")String password) {
        return new ResponseBean(ResponseBean.SUCCESS, "", homePageMapper.getUser(username, password));
    }

}
